package com.javajun.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.javajun.custom.DataGridParam;
import com.javajun.custom.EasyuiUtils;
import com.javajun.custom.Success;
import com.javajun.douban.entity.BookJson;
import com.javajun.entity.Book;
import com.javajun.form.BookForm;
import com.javajun.service.BookService;

@Controller
@RequestMapping(value="/book")
public class BookController {

    @Autowired
    private BookService bookService;
    
    @RequestMapping(value="/search")
    public String searchView(BookForm form, ModelMap map) {
        map.put("model", form);
        return "book/search";
    }
    
    @RequestMapping(value="/bookState", method=RequestMethod.GET)
    public String bookState() {
        return "book/state";
    }
    
    @RequestMapping(value="/addbook")
    @ResponseBody
    public Boolean addBook(String isbn, Integer total) {

        Boolean f = false;
        BookJson bookJson = bookService.getBookJson(isbn);
        
        if (bookJson != null) {
            f = bookService.addBook(bookJson, total);
        }
        return f;
        
    }
    
    @RequestMapping(value="/list.do")
    @ResponseBody
    public Object list(DataGridParam param, BookForm form) {
        Pageable pageable = param.buildPageable();
        Specification<Book> spec = bookService.buildSpec(form);
        Page<Book> page = bookService.findAll(spec, pageable);
        return EasyuiUtils.buildDatagridResult(page);
    }
    
    @RequestMapping(value="/manage")
    public String manage() {
        return "book/manage";
    }
    
    @RequestMapping(value="/show")
    public String showView() {
        return "book/show";
    }
    
    @RequestMapping(value="/save")
    @ResponseBody
    public Object save(BookForm form) {
        Integer id = form.getId();
        Integer total = form.getTotal();
        Integer lend = form.getLend();
        Integer celSur = total - lend;
        Success success = new Success();
        String isbn = form.getIsbn();
        if(celSur >= 0) {
            if (id != null) {
                bookService.modifyBook(id, total);
                success.setSuccess(true);
                success.setMsg("修改"+isbn+"数量为"+total+"本");
            } else {
                
                success.setSuccess(false);
                success.setMsg("修改失败！");
            }
        } else {
            success.setSuccess(false);
            success.setMsg("书的总数不能小于已借出的数量");
        }
        return success;
    }
    
    @RequestMapping(value="/edit", method=RequestMethod.GET)
    public String editView(Integer id, ModelMap map) {
        Book model = new Book();
        if(id != null) {
            model = bookService.findById(id);
            map.put("model", model);
        }
        return "book/edit";
    }
    
    @RequestMapping(value="/check")
    @ResponseBody
    public Object check(BookForm form, ModelMap map) {
        String isbn = form.getIsbn();
        Boolean f = bookService.repeatIsbn(isbn);
        Success success = new Success();
        success.setSuccess(!f);
        if(f) {
            success.setMsg("本书已存在！");
        } else {
            Integer bookStatusCode = bookService.getBookStatusCode(isbn);
            if (bookStatusCode == 200) {
                success.setMsg("该书可以添加");
            } else {
                success.setSuccess(f);
                success.setMsg("该书不存在，请输入正确的ISBN");
            }
        }
        return success;
    }
    
    @RequestMapping(value="/info")
    public String infoView(ModelMap map, String isbn) {
        map.put("isbn", isbn);
        BookJson bookJson = bookService.getBookJson(isbn);
        map.put("model", bookJson);
        return "book/info";
    }
    
    @RequestMapping(value="/edit", method=RequestMethod.POST)
    @ResponseBody
    public Success edit(Integer total, String isbn) {
        Success success = new Success();
        Boolean f = addBook(isbn, total);
        success.setSuccess(f);
        if(f) {
            success.setMsg("添加"+total+"本图书："+isbn+"成功");
        } else {
            success.setMsg("添加图书失败！");
        }
        return success;
    }
    
    @RequestMapping(value="/update", method=RequestMethod.GET)
    public String updateView(ModelMap map, Integer id) {
        Book model = new Book();
        model = bookService.findById(id);
        map.put("model", model);
        return "book/update";
    }
    
    /**
     * 根据Id删除一本书
     * @param id
     * @return
     */
    @RequestMapping(value="/delete")
    @ResponseBody
    public Object delete(Integer id) {
        Book entity = bookService.findById(id);
        String name = entity.getName();
        Success success = new Success();
        try {
            bookService.delete(entity);
            success.setSuccess(true);
            success.setMsg("删除"+name+"成功！");
        } catch (Exception e) {
            e.printStackTrace();
            success.setSuccess(false);
            success.setMsg("删除"+name+"失败！");
        }
        return success;
    }
}
